﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;
using Facebook.Web.FbmlControls;

//Define class that all pages inherit from MasterPage
public partial class MasterPage : CanvasFBMLMasterPage
{
    //On page load this method is activated
    protected void Page_Load(object sender, EventArgs e)
    {
        lutefiskImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/lutefisk.png");
    }

    //On page rendering this method is called
    protected void Page_PreRender(object sender, EventArgs e)
    {
        RequireLogin = true;                                //require the user to be logged in
    }

    //converts a relative URL to Absolute: This is required for facebook compatibility
    //The URL is created as http://facebookappIP/relativeURL
    public static string ConvertRelativeUrlToAbsoluteUrl(Page page, string relativeUrl)
    {
        //Check if user is using SSL or Not using SSL
        if (page.Request.IsSecureConnection)

            //Page.Request.URL.Host returns URI of host as IP
            //page.resolveURL truncates a URL to be usably relative to the requesting host
            return string.Format("https://{0}{1}", page.Request.Url.Host, page.ResolveUrl(relativeUrl));
        else
            return string.Format("http://{0}{1}", page.Request.Url.Host, page.ResolveUrl(relativeUrl));
    }

    //search based on user tags
    protected void searchClick(Object source, EventArgs e)
    {        
        Session["searchTerm"] = searchBox.Text; 
        Lutefisk.DirectPage(Page.Page, "Search");
    }

    //Create advertisement
    protected void advertiseClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "Message");
    }

    //edit the user profile
    protected void editSettingsClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "Form");
    }
    
    //Search for users
    protected void searchButton_Click(object sender, EventArgs e)
    {

    }
}